package cn.jathlon.datastructure;

import java.util.HashMap;

/**
 * 简单的双向map实现
 * @author tuxiao.czz
 *
 * @param <K>
 * @param <V>
 */
@SuppressWarnings("serial")
public class BiMap<K, V> extends HashMap<K, V> {

	private HashMap<V, K> reverseMap = new HashMap<V, K>();
	
	public BiMap(){
		super();
	}
	
	public K getKey(V value) { 
		return reverseMap.get(value); 
	}

	public V put(K key, V value) {
		reverseMap.put(value, key);
		return super.put(key, value);
	}

	// override for efficiency. 
	public boolean containsValue(Object value) { 
		return reverseMap.containsKey(value); 
	}
	
	////////
	public static void main(String[] args) {
		
		BiMap<String, String> biMap = new BiMap<String, String>();
		biMap.put("zhangsan", "1");
		biMap.put("lisi", "2");
		biMap.put("wangwu", "3");
		System.out.println(biMap.get("wangwu"));
		System.out.println(biMap.getKey("2"));
	}
	
}


